package com.meiyu.live.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.chatroom.ChatRoomService;
import com.netease.nimlib.sdk.chatroom.constant.MemberType;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomMember;
import com.netease.nimlib.sdk.chatroom.model.MemberOption;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.meiyu.live.R;
import com.meiyu.live.popj.RoomMessage;
import com.meiyu.live.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 房间管理对话框(场控，禁言)
 */
public class RoomManageDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    private TextView tv_account, btn_set_manager, btn_mute;
    private ImageView iv_avatar;
    private View view_gap;

    private long roomId;
    private String neteaseId;
    private String name;
    private String avatar;

    private boolean isMute, isManager;

    private boolean hasData = false;

    public interface RoomManageListener {
        void onClickFollow();

        void onClickPrivateMessage(String neteaseId);
    }

    private RoomManageListener userInfoListener;

    public RoomManageDialog(Context context) {
        super(context, R.style.dialog_custom);
        init(context);
    }

    public RoomManageDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        setContentView(R.layout.dialog_room_manage);
        setCanceledOnTouchOutside(true);
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_def_header)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.icon_def_header)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.icon_def_header)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();
        tv_account = (TextView) findViewById(R.id.tv_account);
        tv_account.setOnClickListener(this);
        btn_set_manager = (TextView) findViewById(R.id.btn_set_manager);
        btn_set_manager.setOnClickListener(this);
        btn_mute = (TextView) findViewById(R.id.btn_mute);
        btn_mute.setOnClickListener(this);
        iv_avatar = (ImageView) findViewById(R.id.iv_avatar);
        view_gap = findViewById(R.id.view_gap);
    }

    public void setIsAnchor(boolean isAnchor) {
        if (isAnchor) {
            btn_set_manager.setVisibility(View.VISIBLE);
            view_gap.setVisibility(View.VISIBLE);
        } else {
            btn_set_manager.setVisibility(View.GONE);
            view_gap.setVisibility(View.GONE);
        }
    }

    public void setData(long roomId, RoomMessage message) {
        this.roomId = roomId;
        neteaseId = message.getNet_ease_id();
        name = message.getName();
        avatar = message.getAvatar();
        ArrayList<String> accounts = new ArrayList<>();
        accounts.add(neteaseId);
        NIMClient.getService(ChatRoomService.class)
                .fetchRoomMembersByIds(roomId + "", accounts)
                .setCallback(new RequestCallbackWrapper<List<ChatRoomMember>>() {

                    @Override
                    public void onResult(int code, List<ChatRoomMember> result, Throwable exception) {
                        if (result != null && result.size() > 0) {
                            ChatRoomMember member = result.get(0);
                            setIsManager(member.getMemberType() == MemberType.ADMIN);
                            setIsMute(member.isMuted());
                            hasData = true;
                        }
                    }
                });
        update();
    }

    public void setIsManager(boolean isManager) {
        this.isManager = isManager;
        btn_set_manager.setText(isManager ? "取消场控" : "设为场控");
    }

    public void setIsMute(boolean isMute) {
        this.isMute = isMute;
        btn_mute.setText(isMute ? "解除禁言" : "禁止发言");
    }

    public void setUserInfoListener(RoomManageListener userInfoListener) {
        this.userInfoListener = userInfoListener;
    }

    private void update() {
        imageLoader.displayImage(avatar, iv_avatar, options);
        tv_account.setText(name);
    }

    @Override
    public void onClick(View view) {
        if (view == btn_set_manager) {
            if (!hasData) {
                return;
            }
            final boolean managerFlag = isManager;
            NIMClient.getService(ChatRoomService.class)
                    .markChatRoomManager(!managerFlag, new MemberOption(roomId + "", neteaseId))
                    .setCallback(new RequestCallback<ChatRoomMember>() {
                        @Override
                        public void onSuccess(ChatRoomMember param) {
                            isManager = !managerFlag;
                            setIsManager(isMute);
                            dismiss();
                        }

                        @Override
                        public void onFailed(int code) {
                            if (managerFlag) {
                                ToastUtil.show("取消场控失败");
                            } else {
                                ToastUtil.show("设为场控失败");
                            }
                        }

                        @Override
                        public void onException(Throwable exception) {
                            ToastUtil.show("操作发生错误");
                        }
                    });
        } else if (view == btn_mute) {
            if (!hasData) {
                return;
            }
            final boolean muteFlag = isMute;
            MemberOption option = new MemberOption(roomId + "", neteaseId);
            NIMClient.getService(ChatRoomService.class)
                    .markChatRoomMutedList(!muteFlag, option)
                    .setCallback(new RequestCallback<ChatRoomMember>() {
                        @Override
                        public void onSuccess(ChatRoomMember param) {
                            isMute = !muteFlag;
                            setIsMute(isMute);
                            dismiss();
                        }

                        @Override
                        public void onFailed(int code) {
                            if (muteFlag) {
                                ToastUtil.show("解除禁言失败");
                            } else {
                                ToastUtil.show("禁言失败");
                            }
                        }

                        @Override
                        public void onException(Throwable exception) {
                            ToastUtil.show("操作发生错误");
                        }
                    });
        }
    }
}
